package qj.tool.google;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.LinkedList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONSerializer;
import qj.tool.http.WebSession;
import qj.ui.DesktopUI4;
import qj.util.HttpUtil;
import qj.util.funct.P1;
import qj.util.math.Range;

public class Translate {
	public static void main(String[] args) {
		System.out.println(translate1("I love you", "en", "ru"));
//		System.out.println(HttpUtil.formatQueryValue("I love you"));
//		http://translate.google.com/translate_a/t?client=t&sl=en&tl=ru&hl=en&sc=2&ie=UTF-8&oe=UTF-8&oc=1&otf=2&rom=1&ssel=4&tsel=4&q=I%20love%20you
//		http://translate.google.com/translate_a/t?client=t&sl=en&tl=ru&hl=en&sc=2&ie=UTF-8&oe=UTF-8&oc=1&otf=2&rom=1&ssel=4&tsel=4&q=I%20love%20you%20much
	}
	
	public static String translateR(String content, String sourceLanguage, String toLanguage) {
		String url = "http://translate.google.com/translate_a/t?client=t" +
				"&sl="+sourceLanguage +
				"&tl="+toLanguage+"&hl=en&sc=2&ie=UTF-8&oe=UTF-8&oc=1&otf=2&rom=1&ssel=4&tsel=4" +
				"&q=" + content;
		WebSession webSession = new WebSession();
		try {
			HttpURLConnection conn = webSession.conn(url, null, null, new P1<HttpURLConnection>() {public void e(HttpURLConnection obj) {
//				obj.addRequestProperty("Cookie", "TZ=-420; PREF=ID=e0f6de18cbd84586:U=3a64c63b497df4bb:FF=0:LD=en:TM=1338312205:LM=1377084342:GM=1:IG=4:GC=1:S=1MxbQPCshstsGOM9; NID=63=Jv82WJ0a2QRZPvyaQ3xxrlL8NQNwXDltQ-UK7jSHko5Uv547kIv63d5oHn1Zp9e2QE_U_-uZqalLpuCsyzTCeXqegif-_rHe-GCy8qr4Nzl2zcOfjbWab14ymmdqHkGYQKLeLEqeW8gDYNQ5yIkDiVJZJOLKskf5mjlaKjtTo6T03avFaCSuxNR_L0ZqdyxlWSqeHhFFa6GHiTt6S6YnFFbK-ELVJFBGIRP-Ulq92oU; HSID=AYa-fKbs0Kq8zjsGh; APISID=76O_ydZG4bIo2Ltl/AQ0yWdyhW0T_5nAXv; SID=DQAAAHwBAADIk8U0yQAzttXiZMDnqHemKsNQPtD0vTZXS1ROUxW9u3BBJ9rpUPF1kSL2Mt0RHTAA9Zk1JChNa2N5lvrI-6d0_OCCBZbXVWXWe8yXQJ75aegPtohh7jPGtl7KJ11oei3TZtiWUAjryBqfXKWplraBX_FMzTTL9qv4liP1XjMiMPVVuS3rLnocyN-0yM1VyhZiyssni4pcMehurERBQ8c3ScZsHUhU7Dklp8_Fv4z5nYwS2_ibT1BKDdHkJFfIArGjveF0t2kJPrl4Rn7fUr_mfCCSg8EOu4H6XWK2tUvHtd4Uvqx9IFIi5LXrZzGrHJoVHj00s7nmpPrWqoSOo0WEyaHAiCuXzuabKLHeUD46c5l-1MvBpe8-YH0OyhzPxhKe60s6o_OapIcpjhy9hpYsq4epbMsPX2jYyMaR90RzQo9YDgenqFoaYcFU1-lY_bIuW8prllXpgy4J0Rb7oeu3ifL3Fs1ZE24newmjcXi4Rz1DgutNS4lCEbJVwuWDaOQ");
			}});
			return WebSession.toString(conn);
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}
	
	public static List<TBlock> translate(String content, String sourceLanguage, String toLanguage) {
//		String goTraResult = IOUtil.inputStreamToString_force(GTranslate.class.getResourceAsStream("2.json"),"UTF-8");
		String goTraResult = translateR(content,sourceLanguage,toLanguage);
		
//		System.out.println(goTraResult);
		
		return parseGoTraResult(goTraResult);
		
	}

	private static List<TBlock> parseGoTraResult(String goTraResult) {
		JSONArray json = (JSONArray) JSONSerializer.toJSON(goTraResult);

		LinkedList<TBlock> tBlocks = new LinkedList<TBlock>();
		JSONArray variations = json.getJSONArray(5);
		for (int i = 0; i < variations.size(); i++) {
			JSONArray variation = variations.getJSONArray(i);
			String ori = variation.getString(0);
			TBlock block = new TBlock();
			block.ori = ori;
			
			block.translates = new LinkedList<String>();
			JSONArray translates = variation.getJSONArray(2);
			for (int j = 0; j < translates.size(); j++) {
				String translate = translates.getJSONArray(j).getString(0);
//				if (translate.equals(ori)) {
//					continue;
//				}
				block.translates.add(translate);
			}
			if (block.translates.size() > 0) {
				tBlocks.add(block);
			}
		}
		return tBlocks;
	}
	
	public static class TBlock {
		String ori;
		Range range;
		List<String> translates;
	}

	public static String translate1(String content, String sourceLanguage, String toLanguage) {
		String result = translateR(content,sourceLanguage,toLanguage);
		
//		DesktopUI4.alert2(result);

		try {
			JSONArray json = (JSONArray) JSONSerializer.toJSON(result);

			JSONArray jsonArray1 = json.getJSONArray(0).getJSONArray(0);

//			DesktopUI4.alert2(jsonArray1.getString(0));
			return jsonArray1.getString(0);
//			StringBuilder sb = new StringBuilder();
//			for (int i = 0; i < jsonArray1.size(); i++) {
//				JSONArray jsonArray2 = jsonArray1.getJSONArray(i);
//				sb.append(jsonArray2.getString(0));
//			}
//			return sb.toString();
		} catch (JSONException e) {
			System.out.println(result);
			throw new RuntimeException(e);
		}
	}
}
